//
//  YCLHomeTitleBtn.swift
//  新浪微博01
//
//  Created by yaochunlin on 16/4/13.
//  Copyright © 2016年 yaochunlin. All rights reserved.
//

import UIKit

class YCLHomeTitleBtn: UIButton {
    
    init(title: String){
    
        super.init(frame: CGRectZero)
        
        self.setTitle(title, forState: UIControlState.Normal)
        self.setImage(UIImage.init(named: "navigationbar_arrow_down"), forState: UIControlState.Normal)
        self.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        
        self.addTarget(self, action: Selector("rotate:"), forControlEvents: UIControlEvents.TouchUpInside)
        
        self.sizeToFit()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        
        super.layoutSubviews()
        
        titleLabel?.frame.origin.x = 0
        
        imageView?.frame.origin.x = (titleLabel?.frame.size.width)! + 5
    }
    
    @objc private func rotate(btn: YCLHomeTitleBtn) {
        
//        YCLPrint("旋转")
        btn.selected = !btn.selected
        
        UIView.animateWithDuration(YCLDefautlAnimDuration) { 
            
            if btn.selected {
            
                self.imageView?.transform = CGAffineTransformMakeRotation(CGFloat(M_PI - 0.001))
            }else{
            
                self.imageView?.transform = CGAffineTransformIdentity
            }
        }
        
    }
    
    
    
}
